package com.ctrl.riveradministrator.mvp.presenter;

import android.util.Log;

import com.ctrl.riveradministrator.base.BaseObserver;
import com.ctrl.riveradministrator.base.BasePresenter;
import com.ctrl.riveradministrator.RaApplication;
import com.ctrl.riveradministrator.mvp.model.Reach;
import com.ctrl.riveradministrator.mvp.model.Task;
import com.ctrl.riveradministrator.mvp.model.TraceSearch;
import com.ctrl.riveradministrator.mvp.view.activity.trace.TraceView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TracePresenter extends BasePresenter<TraceView> {

    public static final int UPLOAD = 1;
    public static final int START_PATROL = 2;
    public static final int GET_REACH = 3;
    public static final int IS_TRACE = 4;

    public TracePresenter(TraceView baseView) {
        super(baseView);
    }

    public void isTrace(String userid) {
        Map<String, String> map = new HashMap<>();
        map.put("userid", userid);
        attachView(apiService.isTrace(map), new BaseObserver<Integer>(getBaseView()) {
            @Override
            public void onSuccess(Integer model) {
                getBaseView().onSuccess(model, IS_TRACE);
            }

            @Override
            public void onSuccess(List<Integer> model) {
                getBaseView().onSuccess("onSuccess,List", IS_TRACE);
            }

            @Override
            public void onFailed(String msg) {
                getBaseView().onFailed(msg);
            }
        });
    }

    public void focusEnd(String userId) {
        Map<String, String> map = new HashMap<>();
        map.put("userId", userId);
        attachView(apiService.focusEnd(map), new BaseObserver<String>(getBaseView()) {
            @Override
            public void onSuccess(String model) {
                //getBaseView().onSuccess(model, IS_TRACE);
            }

            @Override
            public void onSuccess(List<String> model) {
                //getBaseView().onSuccess("onSuccess,List", IS_TRACE);
            }

            @Override
            public void onFailed(String msg) {
                getBaseView().onFailed(msg);
            }
        });
    }

    public void startPatrol(String userId, String reachId, String reachName,String patrolType) {
        Map<String, String> map = new HashMap<>();
        map.put("userid", userId);
        map.put("reachId", reachId);
        map.put("reachName", reachName);
        map.put("patrolType",patrolType);
        attachView(apiService.startPatrol(map), new BaseObserver<Task>(getBaseView()) {

            @Override
            public void onSuccess(Task model) {
                Log.i("cyc", "startPatrol----onSuccess");
                getBaseView().onSuccess(model, START_PATROL);
            }

            @Override
            public void onSuccess(List<Task> model) {
                Log.i("cyc", "startPatrol----onSuccess");
                getBaseView().onSuccess("onSuccess List<Task>", START_PATROL);
            }

            @Override
            public void onFailed(String msg) {
                Log.i("cyc", "startPatrol----onFailed" + msg);
            }

        });
    }

    public void upload(String point, String userId) {
        Map<String, String> map = new HashMap<>();
        map.put("track", point);
        map.put("userId", userId);
        Log.d("cyc", "upload ----->" + map.toString());
        attachViewSynchron(apiService.uploadPatrol(map), new BaseObserver<String>(getBaseView()) {
            @Override
            public void onSuccess(String model) {
                Log.d("onSuccess", "upload");
                getBaseView().onSuccess("onSuccess", UPLOAD);
            }

            @Override
            public void onSuccess(List<String> model) {
                Log.d("onSuccess", "upload");
                getBaseView().onSuccess("onSuccess,List", UPLOAD);
            }

            @Override
            public void onFailed(String msg) {
                Log.d("onFail", "failed:" + msg);
                getBaseView().onFailed(msg);
            }
        });
    }

    public void getReach() {
        Map<String, String> map = new HashMap<>();
        map.put("userId", RaApplication.getUserId());
        attachView(apiService.getReach(map), new BaseObserver<Reach>(getBaseView()) {

            @Override
            public void onSuccess(Reach model) {

            }

            @Override
            public void onSuccess(List<Reach> model) {
                Log.d("cyc", "河流getReach: " + model);
                getBaseView().getReach(model);
            }

            @Override
            public void onFailed(String msg) {
                Log.d("cyc", "河流failed::" + msg);
                //getBaseView().onFailed(msg);
            }
        });
    }

    public void getReservoir() {
        Map<String, String> map = new HashMap<>();
        map.put("userId", RaApplication.getUserId());
        attachView(apiService.getReservoirById(map), new BaseObserver<Reach>(getBaseView()) {

            @Override
            public void onSuccess(Reach model) {

            }

            @Override
            public void onSuccess(List<Reach> model) {
                Log.d("cyc", "水库getReservoir: " + model);
                getBaseView().getReservoir(model);
            }

            @Override
            public void onFailed(String msg) {
                Log.d("cyc", "水库getReservoir:" + msg);
                //getBaseView().onFailed(msg);
            }
        });
    }

    public void trace(String patrolId) {
        Map<String, String> map = new HashMap<>();
        map.put("patrolId", patrolId);
        attachView(apiService.traceHistory(map), new BaseObserver<TraceSearch>(getBaseView()) {

            @Override
            public void onSuccess(TraceSearch model) {

            }

            @Override
            public void onSuccess(List<TraceSearch> model) {
                Log.d("onSuccess", "trace");
                getBaseView().traceHistory(model);
            }

            @Override
            public void onFailed(String msg) {
                Log.d("onFail", "failed:" + msg);
                getBaseView().onFailed(msg);
            }
        });
    }
}
